package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.ProjectWarehouse;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.ProjectParkMapper;
import com.ruoyi.industryfrontend.mapper.ProjectWarehouseMapper;
import com.ruoyi.industryfrontend.mapper.SpaceCollectMapper;
import com.ruoyi.industryfrontend.mapper.SpaceIndustryMapper;
import com.ruoyi.industryfrontend.service.IProjectWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目-仓库Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@Service
public class ProjectWarehouseServiceImpl implements IProjectWarehouseService
{
    @Autowired
    private ProjectWarehouseMapper projectWarehouseMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询项目-仓库
     *
     * @param id 项目-仓库主键
     * @return 项目-仓库
     */
    @Override
    public ProjectWarehouse selectProjectWarehouseById(Long id)
    {
        return projectWarehouseMapper.selectProjectWarehouseById(id);
    }

    /**
     * 根据编号查询仓库
     * @param spaceId 编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectProjectWarehouseByNumber(String spaceId) {
        ProjectWarehouse projectWarehouse = projectWarehouseMapper.selectProjectWarehouseByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectWarehouse.setCollectState(1L);
        }else {
            projectWarehouse.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectWarehouse);
        if (projectWarehouse.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectWarehouse.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询项目-仓库列表
     *
     * @param projectWarehouse 项目-仓库
     * @return 项目-仓库
     */
    @Override
    public List<ProjectWarehouse> selectProjectWarehouseList(ProjectWarehouse projectWarehouse)
    {
        return projectWarehouseMapper.selectProjectWarehouseList(projectWarehouse);
    }
    @Override
    public List<ProjectWarehouse> selectProjectWarehouseListz(ProjectWarehouse projectWarehouse)
    {
//        if (projectWarehouse.getWareEmergency() != null && projectWarehouse.getWareEmergency().equals("H")) {
//            projectWarehouse.setWareEmergency("有");
//        } else if (projectWarehouse.getWareEmergency() != null && projectWarehouse.getWareEmergency().equals("N")) {
//            projectWarehouse.setWareEmergency("无");
//        }
        if (projectWarehouse.getWareSellPrice()!=null){
            String[] split = projectWarehouse.getWareSellPrice().split("~");
            projectWarehouse.setWareSellPriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectWarehouse.setWareSellPriceHigh(Long.parseLong(split[1]));
            }
        }
        if(projectWarehouse.getWareLeasePrice()!=null){
            String[] split = projectWarehouse.getWareLeasePrice().split("~");
            projectWarehouse.setWareLeasePriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectWarehouse.setWareLeasePriceHigh(Long.parseLong(split[1]));
            }
        }
        projectWarehouse.setSupplyUserId(SecurityUtils.getUserId());
        return projectWarehouseMapper.selectProjectWarehouseListz(projectWarehouse);
    }

    /**
     * 新增项目-仓库
     *
     * @param projectWarehouse 项目-仓库
     * @return 结果
     */
    @Override
    public int insertProjectWarehouse(ProjectWarehouse projectWarehouse)
    {
        projectWarehouse.setSupplyUserId(SecurityUtils.getUserId());//发布人ID
        projectWarehouse.setApprovalState(0L);//审核
        projectWarehouse.setCreateTime(DateUtils.getNowDate());
        projectWarehouse.setSerialNumber(IdUtils.fastSimpleUUID());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectWarehouse, spaceIndustry);
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectWarehouse.getCreateTime());
        spaceIndustry.setSpaceId(projectWarehouse.getSerialNumber());
        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
        return projectWarehouseMapper.insertProjectWarehouse(projectWarehouse);
    }
    private static void extracted(ProjectWarehouse projectWarehouse, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(4);
        spaceIndustry.setReleaseType(0L);//供给
        spaceIndustry.setApprovalState(projectWarehouse.getApprovalState());
        spaceIndustry.setProvinceId(projectWarehouse.getProvinceId());//省份
        spaceIndustry.setCityId(projectWarehouse.getCityId());//城市
//        spaceIndustry.setAreaId();//区域
        spaceIndustry.setSupplyType(projectWarehouse.getSupplyType());//服务类型
        spaceIndustry.setParkId(String.valueOf(projectWarehouse.getParkId()));
        spaceIndustry.setHeating(projectWarehouse.getWareHeating());//供暖
        spaceIndustry.setRefrig(projectWarehouse.getWareRefrig());//制冷
        spaceIndustry.setPower(projectWarehouse.getWarePower());//电力
        spaceIndustry.setGas(projectWarehouse.getWareGas());//燃气
        spaceIndustry.setLeasePrice(projectWarehouse.getWareLeasePrice());//出售价格
        spaceIndustry.setSellPrice(projectWarehouse.getWareSellPrice());//租赁价格
        spaceIndustry.setCoverResource(projectWarehouse.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectWarehouse.getWareName());//园区名称
        spaceIndustry.setSpaceAllSize(projectWarehouse.getWareAllSize());//空间总面积
        spaceIndustry.setTop(projectWarehouse.getTop());//置顶

    }
    /**
     * 修改项目-仓库
     *
     * @param projectWarehouse 项目-仓库
     * @return 结果
     */
    @Override
    public int updateProjectWarehouse(ProjectWarehouse projectWarehouse)
    {
        projectWarehouse.setApprovalState(0L);//未审核
        projectWarehouse.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectWarehouse,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectWarehouse.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectWarehouse.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByWarehouseId(spaceIndustry);
        return projectWarehouseMapper.updateProjectWarehouse(projectWarehouse);
    }

    /**
     * 批量删除项目-仓库
     *
     * @param ids 需要删除的项目-仓库主键
     * @return 结果
     */
    @Override
    public int deleteProjectWarehouseByIds(Long[] ids)
    {
        return projectWarehouseMapper.deleteProjectWarehouseByIds(ids);
    }

    /**
     * 删除项目-仓库信息
     *
     * @param id 项目-仓库主键
     * @return 结果
     */
    @Override
    public int deleteProjectWarehouseById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectWarehouseMapper.deleteProjectWarehouseById(id);
    }

    @Override
    public int deleteProjectWarehouseBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectWarehouseMapper.deleteProjectWarehouseBySerialNumber(spaceId);
    }
}
